// 板宽度、高度
const SIC_W = 300;
const SIC_H = 60;
const WSIC_W = 600;
const WSIC_H = 60;
const XSIC_W = 600;
const XSIC_H = 120;
const FAN_W = 110;
const FAN_H = 240;
const BOX_BORDER_SIZE = 2;
const BOX_BORDER_COLOR = "#aeaeae";
const LED_LEFT = 0;
const LED_RIGHT = 1;
const LED_ALL = 2;
const LED_GREEN = "#1cca03";
const LED_YELLOW = "#f9e000";
const LED_GRAY = "#aeaeae";

function getBrdFilename(brdName) {
    // 用下划线替换单板名称中的特殊字符
    return brdName.replaceAll('/', '_');
}

// 根据端口获取槽位号
function getPortSlot(portName) {
    let number = portName.match(/[0-9][0-9]*\//g);
    return (number && number.length > 0) ? parseInt(number[0].replaceAll('/', '').trim()) : undefined;
}

// 根据端口获取端口号
function getPortId(portName) {
    let number = portName.match(/\/[0-9][0-9]*/g);
    return (number && number.length > 0) ? parseInt(number[number.length - 1].replaceAll('/', '').trim()) : undefined;
}

// 构造2/3段式端口标识
function setup2SegPortIds(type, logicSlot, portNum) {
    switch (type) {
        case "GE": {
            return [
                "GigabitEthernet " + logicSlot + "/" + portNum,
                "GE " + logicSlot + "/" + portNum,
                "GigabitEthernet" + logicSlot + "/" + portNum,
                "GE" + logicSlot + "/" + portNum,
            ];
        }
        case "ETH": {
            return [
                "Ethernet " + logicSlot + "/" + portNum,
                "ETH " + logicSlot + "/" + portNum,
                "Ethernet" + logicSlot + "/" + portNum,
                "ETH" + logicSlot + "/" + portNum,
            ];
        }
        default: {
            return undefined;
        }
    }
}
function setup3SegPortIds(type, logicSlot, portNum) {
    switch (type) {
        case "GE": {
            return [
                "GigabitEthernet " + logicSlot + "/0/" + portNum,
                "GE " + logicSlot + "/0/" + portNum,
                "GigabitEthernet" + logicSlot + "/0/" + portNum,
                "GE" + logicSlot + "/0/" + portNum,
            ];
        }
        case "ETH": {
            return [
                "Ethernet " + logicSlot + "/0/" + portNum,
                "ETH " + logicSlot + "/0/" + portNum,
                "Ethernet" + logicSlot + "/0/" + portNum,
                "ETH" + logicSlot + "/0/" + portNum,
            ];
        }
        default: {
            return undefined;
        }
    }
}

// 画光接口
function drawOptical(pos_x, pos_y, portIds, panel, setting) {
    panel.children.push({
        type: 'rect',
        left: pos_x,
        top: pos_y,
        shape: {width: 43, height: 32},
        style: {fill: "rgba(215, 215, 215, .1)"},
        scaleX: 1,
        scaleY: 1,
        portType: "optical",
        portIds: portIds,
        onmouseover: function (ev) {
            setting.onMouseOverPort(portIds[0], ev);
        },
        onmouseout: function (ev) {
            setting.onMouseOutPort(portIds[0], ev);
        },
        onclick: function (ev) {
            setting.onClickPort(portIds[0], ev);
        }
    });
}

// 画电接口
function drawFabric(pos_x, pos_y, portIds, panel, setting, opt) {
    let led_w = 10;
    let led_h = 7;
    panel.children.push({
        type: 'group',
        left: pos_x,
        top: pos_y,
        scaleX: 1,
        scaleY: 1,
        portType: "fabric",
        portIds: portIds,
        children: [
            // 左灯
            {
                type: 'rect',
                left: opt.yellow_x,
                top: opt.yellow_y,
                shape: {width: led_w, height: led_h},
                style: {fill: "" + LED_YELLOW}
            },
            // 中间部分
            {
                type: 'rect',
                left: 0,
                top: 0,
                shape: {width: 43, height: 36},
                // style: {fill: "rgba(215, 0, 0, .3)"}, // ONLY for DEBUGGING
                style: {fill: "rgba(215, 215, 215, .1)"},
                onmouseover: function (ev) {
                    setting.onMouseOverPort(portIds[0], ev);
                },
                onmouseout: function (ev) {
                    setting.onMouseOutPort(portIds[0], ev);
                },
                onclick: function (ev) {
                    setting.onClickPort(portIds[0], ev);
                }
            },
            // 右灯
            {
                type: 'rect',
                left: opt.green_x,
                top: opt.green_y,
                shape: {width: led_w, height: led_h},
                style: {fill: LED_GREEN}
            }
        ]
    });
}

// 更新接口 LED
function updatePortLed(portName, ledPos, ledColor, portSet) {
    for (let i = 0; i < portSet.length; i++) {
        // 查找到端口并更新 LED 状态
        let thisPort = portSet[i];
        if ((thisPort.portIds)&&(thisPort.portIds.includes(portName))) {
            if (thisPort.portType === 'fabric') { // fabric port
                let leftLed = thisPort.children[0];
                let rightLed = thisPort.children[2];
                if (ledPos === LED_LEFT) {
                    leftLed.style.fill = ledColor;
                } else if (ledPos === LED_RIGHT) {
                    rightLed.style.fill = ledColor;
                } else if (ledPos === LED_ALL) {
                    leftLed.style.fill = ledColor;
                    rightLed.style.fill = ledColor;
                }
                return;
            } else {
                // optical port
            }
        }
    }
}

export {
    SIC_W,
    SIC_H,
    WSIC_W,
    WSIC_H,
    XSIC_W,
    XSIC_H,
    FAN_W,
    FAN_H,
    BOX_BORDER_COLOR,
    BOX_BORDER_SIZE,
    LED_LEFT,
    LED_RIGHT,
    LED_ALL,
    LED_GREEN,
    LED_YELLOW,
    LED_GRAY,
    getBrdFilename,
    setup2SegPortIds,
    setup3SegPortIds,
    getPortSlot,
    drawOptical,
    drawFabric,
    updatePortLed,
}

